#include "my_tabbar_style.h"

MyTabBarStyle::MyTabBarStyle(Qt::Orientation orientation){
    m_oriHV = orientation;
}

MyTabBarStyle::~MyTabBarStyle()
{

}

void MyTabBarStyle::drawControl(ControlElement element, const QStyleOption *option, QPainter *painter, const QWidget *widget) const
{
    // Calling the drawing control function of the parent class
    QProxyStyle::drawControl(element, option, painter, widget);

    // Redraw tab label text
    if (CE_TabBarTabLabel == element) {
        if (const QStyleOptionTab *pTab = qstyleoption_cast<const QStyleOptionTab *>(option)) {
            QRect controlRect = pTab->rect;

            QString sTabText;
            if (Qt::Vertical == m_oriHV) {
                for (int i=0; i<pTab->text.length(); i++) {
                    sTabText.append(pTab->text.at(i));
                    sTabText.append('\n');
                }

                if (1 < sTabText.length()) {
                    sTabText = sTabText.mid(0, sTabText.length() - 1);
                }
            }
            else {
                sTabText = pTab->text;
            }

            QTextOption option;
            option.setAlignment(Qt::AlignCenter);
            QPen pen(Qt::blue, 2, Qt::SolidLine);
//            QPen pen = painter->pen();
//            pen.setColor(pTab->palette.color(QPalette::WindowText));
            painter->setPen(pen);
            QFont font = painter->font();
            font.setPointSize(14);
            painter->setFont(font);
            painter->drawText(controlRect, sTabText, option);
        }
    }
}

void MyTabBarStyle::drawItemText(QPainter *painter, const QRect &rect, int flags, const QPalette &pal, bool enabled, const QString &text, QPalette::ColorRole textRole) const
{
    Q_UNUSED(painter);
    Q_UNUSED(rect);
    Q_UNUSED(flags);
    Q_UNUSED(pal);
    Q_UNUSED(enabled);
    Q_UNUSED(text);
    Q_UNUSED(textRole);

    // Function body, drawing text in the drawControl function
}

QSize MyTabBarStyle::sizeFromContents(ContentsType type, const QStyleOption *option, const QSize &size, const QWidget *widget) const
{
    Q_UNUSED(option);
    Q_UNUSED(widget);

    QSize sizeNew = size;
    if (CT_TabBarTab == type) {
        if (Qt::Vertical == m_oriHV) {
            sizeNew.rwidth() += 20;
            sizeNew.rheight() += 20;
        }
        else {
            // Qt::Horizontal, Rotate text 90 degrees
            sizeNew.transpose();
            sizeNew.rwidth() += 20;
            sizeNew.rheight() += 35;
        }
    }

    return sizeNew;
}
